/*
 * @Description: 
 * @version: 1.0
 * @Author: ziwei
 * @Date: 2021-08-22 09:43:43
 * @LastEditors: ziwei
 * @LastEditTime: 2021-08-26 07:59:43
 */
var webpack = require('webpack');
const path = require('path');
const HTMLWebpackPlugin = require('html-webpack-plugin')
module.exports = (env) => {
    console.log(env)
    return {
        entry: './src/index.js',
        output: {
            path: path.resolve(__dirname, 'dist'), //默认静态文件的根目录
            filename: 'main.js'
        },
        devServer: {
            port: 8080, //配置http服务预览的端口号
            open: true,
            compress: true, //是否启用压缩
            static: path.resolve(__dirname, 'public') //额外的静态文件的跟目录配置dist是没有意义的
        },
        module: {
            rules: [{
                    test: /\.js$/,
                    use: [{
                        loader: 'babel-loader',
                        options: {
                            presets: [],
                            plugins: [
                                ['@babel/plugin-proposal-decreto']
                            ]
                        }
                    }]
                },
                //use 的意思是哪些loader进行转换，转换顺序从右向左
                //最右边的loader
                {
                    test: /\.css$/,
                    use: [
                        "style-loader",
                        "css-loader"
                    ]
                }, {
                    //less-loader less转成css css-loader
                    test: /\.less$/,
                    use: [
                        "style-loader",
                        "css-loader",
                        "less-loader"
                    ]
                }
            ]
        },
        plugins: [
            new HTMLWebpackPlugin({
                template: './src/index.html'
            }),
            new webpack.DefinePlugin({
                'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
            })
        ]
    }
}

// 1:
// 2:env node中取不到
//cross-env  set
//node  node里可以取到，其他地方都取不到，配置文件参数，模块内都取不到

//webpack.DefinePlugin 字符串替换 JSON.stringify 才不是变量